package com.threeti.mecool.core.domain.model.project;

import java.util.Collections;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.apache.commons.lang.Validate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.threeti.mecool.core.domain.model.plan.pojo.Job;
import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;
import com.threeti.mecool.framework.util.Lists;

/** 项目类型 */
@Entity
@Table(name = "DM_PROJECT_CATEGORY")
public class ProjectCategory extends DefaultDomainEntity {
  
  @Column(unique=true)
  private String name;
  
  @ManyToMany(fetch=FetchType.EAGER)
  @JoinTable(name = "DM_PROJECTCATEGORY_WFJOB", joinColumns = { @JoinColumn(name = "PROJECT_CATEGORY_ID") }, inverseJoinColumns = { @JoinColumn(name = "WF_JOB_ID") })
  @Fetch(FetchMode.SUBSELECT)  
  private List<Job> jobs = Lists.newArrayList(); 

  public ProjectCategory(String name) {
    super();
    this.name = name;
  }

  /** 项目类型名称 */
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
	this.name = name;
  }

  /** 项目类型包含任务(模板) */
  public List<Job> getJobs() {
    return jobs;
  }

  /** 往项目类型里添加任务(模板) */
  public ProjectCategory addJob(Job job) {
    Validate.notNull(job, "任务不能为空");
    Validate.notNull(job.getId(), "只允许添加已经存在的任务");
    
    jobs.add(job);
    return this;
  }

  ProjectCategory() {
  }

  @Override
  public String toString() {
    return "ProjectCategory [name=" + name + "]";
  }

  /** 强制刷新项目类型包含的任务(模板) */
  public void setJobs(List<Job> jobs) {
    this.jobs = jobs;
  }
  
  
}
